package com.haircut.project.utils.record;

import cn.hutool.core.util.ObjectUtil;
import com.haircut.project.model.config.ProjectConfig;
import com.modular.constant.NumberConstant;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Optional;

public class RecordUtils {
    /**
     * 计算公司分成
     * @param relAmount
     * @param config
     * @return
     */
    public static BigDecimal calcComapy(BigDecimal relAmount, ProjectConfig config){
        if(ObjectUtil.equal(relAmount,BigDecimal.ZERO)){
            return BigDecimal.ZERO;
        }
        BigDecimal HUNDRED = new BigDecimal(NumberConstant.HUNDRED);
        return  relAmount.multiply(HUNDRED.subtract(config.getTechnicianRate())).divide(HUNDRED,NumberConstant.TWO, RoundingMode.HALF_UP);
    }
    /**
     * 计算公司分成
     * @param relAmount
     * @param config
     * @return
     */
    public static BigDecimal calcTechnician(BigDecimal relAmount, ProjectConfig config){
        if(ObjectUtil.equal(relAmount,BigDecimal.ZERO)){
            return BigDecimal.ZERO;
        }

        return  calcAmountRate(relAmount,config.getTechnicianRate());
    }

    /**
     * 计算公司分成
     * @param relAmount
     * @param rate
     * @return
     */
    public static BigDecimal calcAmountRate(BigDecimal relAmount, BigDecimal rate){
        rate = Optional.ofNullable(rate).orElse(BigDecimal.ZERO);
        if(ObjectUtil.equal(relAmount,BigDecimal.ZERO)){
            return BigDecimal.ZERO;
        }
        BigDecimal HUNDRED = new BigDecimal(NumberConstant.HUNDRED);
        return  relAmount.multiply(rate).divide(HUNDRED,NumberConstant.TWO, RoundingMode.HALF_UP);
    }

    public static void main(String[] args) {
        ProjectConfig config = new ProjectConfig();
        config.setTechnicianRate(BigDecimal.valueOf(0.1));
        BigDecimal relAmount = new BigDecimal(64);
        System.out.println(relAmount);
        System.out.println(RecordUtils.calcComapy(relAmount,config));
        System.out.println(RecordUtils.calcTechnician(relAmount,config));
    }
}
